var mysql = require('mysql');
var connection = mysql.createConnection({
    host: '101.200.89.202',
    user: 'root',
    port: '3306',
    password: '123456',
    database:'diabetes'
});

connection.connect();

exports.register = function (email,pass,name,account,phone,create_time) {
        create_time = new Date();
        var modSql = "INSERT INTO l_huangke (email,name,pass,account,phone,create_time) VALUES (?,?,?,?,?,?)";
        var modSqlParams = [email,name,pass,account,phone,create_time];
        connection.query(modSql,modSqlParams, function(err, rows) {
            if (err) throw err;
        });
        return account;
    };
exports.login = function (email,pass) {
        var query = new Promise(function (resolve) {
            connection.query("select * from l_huangke where email = '"+ email + "' and pass = '"+pass+"' limit 1", function(err, rows) {
                if (err) throw err;
                else if(rows.length == 0) resolve(-1);
                else resolve(rows);
            });
        })
        return query;
    };
exports.getNext = function () {
        var query = new Promise(function (resolve,reject) {
            connection.query('select id from l_huangke order by create_time desc limit 1', function (err, rows) {
                if (err) reject(err);
                else if (rows.length == 0) resolve(0);
                else resolve(rows[0].id);

            });
        })

        return query;
    };


exports.saveComment = function (fromAccount,toAccount,star,content) {
    var create_time = new Date();
    var modSql = "INSERT INTO l_huangke_comment (from_account,to_account,create_time,content,star) VALUES (?,?,?,?,?)";
    var modSqlParams = [fromAccount,toAccount,create_time,content,star];
    connection.query(modSql,modSqlParams, function(err, rows) {
        if (err) throw err;
    });
};


